<template>
  <div class="GoodsList">
    <div class="goodsList_text">
      <el-input placeholder="请输入内容" v-model="inpval" class="input-with-select" @input="out">
        <el-button slot="append" @click="search" icon="el-icon-search"></el-button>
      </el-input>
      <el-button class="add" type="primary" @click="addGoodList">添加商品</el-button>
      <el-table :data="tableData" border style="width: 100%" height="505px">
        <el-table-column type="index"> </el-table-column>
        <el-table-column prop="goods_name" label="商品名称"> </el-table-column>
        <el-table-column prop="goods_price" label="商品价格"> </el-table-column>
        <el-table-column prop="goods_weight" label="商品重量">
        </el-table-column>
        <el-table-column prop="add_time" label="创建时间">
          <template slot-scope="scope">
            <span>{{ scope.row.add_time | newtime }}</span>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作">
          <template slot-scope="scope">
            <el-button @click="handleClick(scope.row)" type="text" size="small">编辑</el-button>
            <el-button type="text" size="small" @click="delGood(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页作业 -->
      <div class="baclk">
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
          :page-sizes="[5, 10, 15, 20]" :page-size="page.pagesize" layout="total, sizes, prev, pager, next, jumper"
          :total="allDate">
        </el-pagination>
      </div>
    </div>
    <!-- 弹窗 -->
    <el-dialog title="提示" :visible.sync="dialogVisible" width="30%">
      <span>
        <el-form ref="form" :model="form" label-width="80px">
          <el-form-item label="名称">
            <el-input v-model="form.goods_name"></el-input>
          </el-form-item>
          <el-form-item label="价格">
            <el-input v-model="form.goods_price"></el-input>
          </el-form-item>
          <el-form-item label="重量">
            <el-input v-model="form.goods_weight"></el-input>
          </el-form-item>
          <el-form-item label="数量">
            <el-input v-model="form.goods_number"></el-input>
          </el-form-item>
        </el-form>
      </span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addupdate">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getGoods, getdelgood, updateGetgood } from "@/utils/http";
export default {
  data() {
    return {
      // 输入框数据
      inpval: "",
      tableData: [],
      dialogVisible: false,
      allDate: 0,
      page: {
        pagenum: 1,
        pagesize: 10,
        query: "",
      },
      form: {
        goods_name: "",
        goods_price: "",
        goods_number: "",
        goods_weight: "",
      },
      myId: "",
    };
  },
  created() {
    this.getreder();
  },
  methods: {
    // 编辑
    handleClick(val) {
      this.dialogVisible = true;
      this.myId = val.goods_id;
      this.form = val;
    },
    // 点击确定
    addupdate() {
      this.dialogVisible = false;
      updateGetgood(this.myId, this.form).then((res) => {
        console.log(res);
        this.getreder();
      });
    },
    // 输入框
    out() {
      if (this.inpval == "") {
        this.getreder();
      }
    },
    // 封装数据
    getreder() {
      getGoods(this.page).then((res) => {
        console.log(res);
        this.tableData = res.data.goods;
        this.allDate = res.data.total;
      });
    },
    // 点击搜索
    search() {
      this.tableData = this.tableData.filter((ele) => {
        return ele.goods_name.includes(this.inpval);
      });
    },
    handleSizeChange(val) {
      this.page.pagesize = val;
      this.getreder();
    },
    handleCurrentChange(val) {
      this.page.pagenum = val;
      this.getreder();
    },
    // 点击跳转到添加页面
    addGoodList() {
      this.$router.push("/addGoodList");
    },
    // 点击删除
    delGood(val) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          getdelgood({ id: val.goods_id }).then((res) => {
            this.$message({
              type: "success",
              message: "删除成功!",
            });
            this.getreder();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
  filters: {
    newtime(val) {
      return new Date(val * 1000).toLocaleString();
    },
  },
};
</script>

<style lang="scss" scoped>
.baclk {
  margin-top: 10px;
}

.GoodsList {
  padding: 10px;
  background: white;
  border-radius: 8px;

  .goodsList_text {
    .add {
      margin-left: 15px;
    }

    .input-with-select {
      width: 350px;
      margin-bottom: 10px;
    }
  }
}
</style>